package othello.model;

import java.util.Arrays;

public class State {
	private final byte[] matrix;
	
	public State(byte[][] matrix) {
		this.matrix = to1D(matrix);
	}
	
	@Override
	public boolean equals(Object obj) {
		boolean equals = true;
		if (!(obj instanceof State)) {
			equals = false;
		} else {
			State state = (State) obj;
			equals = Arrays.equals(this.matrix, state.matrix);
		}
		return equals;
	}
	
	@Override
	public int hashCode() {
		return 1;
	}
	
	public double[] toDoubles() {
        double[] doubles = new double[this.matrix.length];
        for (int idx = 0; idx != doubles.length; idx++){
            if (this.matrix[idx] == 1){
                doubles[idx] = 1.0;
            }else if (this.matrix[idx] == -1){
                doubles[idx] = -1.0;
            }else if (this.matrix[idx] == 0){
                doubles[idx] = 0.0;
            }        
        }
        return doubles;
    }
        
	
	byte[] to1D(byte[][] matrix) {
		byte[] bytes = new byte[matrix.length * matrix[0].length];		
		for(int index = 0;index < matrix.length;index++) {
			System.arraycopy(matrix[index], 0, bytes, matrix[index].length * index, matrix[index].length);
		}		
		return bytes;
	}
}
